/*
 * Copyright (c) 2019-2020, https://api.iot.10086.cn/(https://gitee.com/cmcc-iot-api/iot-card-open-application/).
 */
package cn.iot.card.open.sim.controller;

import cn.iot.card.open.constants.ErrorCode;
import cn.iot.card.open.context.LoginContextHolder;
import cn.iot.card.open.context.model.LoginUser;
import cn.iot.card.open.exception.enums.BizExceptionEnum;
import cn.iot.card.open.modular.entity.Card;
import cn.iot.card.open.modular.entity.UserGroupRl;
import cn.iot.card.open.modular.service.CardService;
import cn.iot.card.open.modular.service.UserService;
import cn.iot.card.open.pojo.page.LayuiPageFactory;
import cn.iot.card.open.sim.dto.UsageReqDTO;
import cn.iot.card.open.sim.service.*;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.Map;

/**
 * 物联卡历史用量菜单
 *
 * @author cmcc-iot-api
 * @date 20200221 09:54:12
 */
@Controller
@RequestMapping("/history_usage")
public class HistoryUsageController {

    /**
     * 页面路径前缀
     */
    private static String PREFIX_PAGE = "/modular/sim/history_usage/";

    @Autowired
    private CardService cardService;

    @Autowired
    private UserService userService;

    @Autowired
    private GprsUsageDailyService gprsUsageDailyService;

    @Autowired
    private GprsUsageMonthlyService gprsUsageMonthlyService;

    @Autowired
    private SmsUsageDailyService smsUsageDailyService;

    @Autowired
    private SmsUsageMonthlyService smsUsageMonthlyService;

    @Autowired
    private VoiceUsageDailyService voiceUsageDailyService;

    @Autowired
    private VoiceUsageMonthlyService voiceUsageMonthlyService;

    /**
     * 查询接口类型 0-GPRS
     */
    private static final String QUERYTYPE_0 = "0";

    /**
     * 查询接口类型 1-短信
     */
    private static final String QUERYTYPE_1 = "1";

    /**
     * 查询日期类型 0-按日查询，1-按月查询
     */
    private static final String DATETYPE0 = "0";

    private static final String ACCOUNTID = "accountId";

    /**
     * 跳转到物联卡历史用量页面
     *
     * @return 返回 string
     * @author cmcc-iot-api
     * @date 20200221 18:20:50
     */
    @RequestMapping("")
    public String index() {
        return PREFIX_PAGE + "history_usage.html";
    }


    /**
     * 跳转到物联卡历史用量列表
     *
     * @param usageReqDTO 页面查询条件对象
     * @return 返回 object
     * @throws IOException
     * @author cmcc-iot-api
     * @date 20200221 18:19:41
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(UsageReqDTO usageReqDTO) throws IOException {
        // 卡号校验
        String msisdn = usageReqDTO.getMsisdn();
        String iccid = usageReqDTO.getIccid();
        String imsi = usageReqDTO.getImsi();
        Card card;
        if (ToolUtil.isNotEmpty(msisdn) || ToolUtil.isNotEmpty(iccid) || ToolUtil.isNotEmpty(imsi)) {
            card = cardService.verifyCardInfo(msisdn, iccid, imsi);
            if (null == card) {
                // 卡号信息不对应，返回空
                return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
            }
            // 验证当前用户是否有卡权限
            LoginUser loginUser = LoginContextHolder.getContext().getUser();
            UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
            if (null == userGroupRl) {
                return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_BELONG_USER);
            }

        } else {
            return LayuiPageFactory.createEmptyPageInfo();
        }
        // 根据卡号查账户
        Map<String, Object> cardDtoMap = cardService.getCardInfo(card.getCardId());
        Object accountIdObj = cardDtoMap.get(ACCOUNTID);
        if (ToolUtil.isEmpty(accountIdObj)) {
            // 当前卡未绑定任何账户
            return LayuiPageFactory.cteateErrorMsg(ErrorCode.ERR11047);
        }
        usageReqDTO.setAccountId(String.valueOf(accountIdObj));
        // GPRS 接口
        if (QUERYTYPE_0.equals(usageReqDTO.getQueryType())) {
            // 查询日期类型 0-按日查询
            if (DATETYPE0.equals(usageReqDTO.getDateType())) {
                return gprsUsageDailyService.queryGprsUsageDaily(usageReqDTO);
            } else {
                // 查询日期类型 1-按月查询
                return gprsUsageMonthlyService.queryGprsUsageMonthly(usageReqDTO);
            }

        } else if (QUERYTYPE_1.equals(usageReqDTO.getQueryType())) {
            // 短信 接口
            // 查询日期类型 0-按日查询
            if (DATETYPE0.equals(usageReqDTO.getDateType())) {
                return smsUsageDailyService.querySmsUsageDaily(usageReqDTO);
            } else {
                // 查询日期类型 1-按月查询
                return smsUsageMonthlyService.querySmsUsageMonthly(usageReqDTO);
            }
        } else {
            // 语音 接口
            // 查询日期类型 0-按日查询
            if (DATETYPE0.equals(usageReqDTO.getDateType())) {
                return voiceUsageDailyService.queryVoiceUsageDaily(usageReqDTO);
            } else {
                // 查询日期类型 1-按月查询
                return voiceUsageMonthlyService.queryVoiceUsageMonthly(usageReqDTO);
            }
        }
    }

}
